package com.Sdebruijn.curriculumvitae.models;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * Sub Class of {@link Person} representing reference person which can be
 * contacted
 * 
 * @author Stefan de Bruijn
 */
@DatabaseTable(tableName = "Reference")
public class Reference extends Person {

	Reference() {
		// all persisted classes must define a no-arg constructor with at least
		// package visibility
	}

	/**
	 * Describes the connection this {@link Person} had with me in the
	 * referenced institution
	 */
	@DatabaseField
	@SerializedName("connection")
	public String connection;

	/*
	 * Required methods to have this class as Parcelable Having this class
	 * Parcelable will make it easier to restore it into fragments with
	 * savedInstanceState
	 */

	/*
	 * Just used as id - hashCode() will do fine
	 * 
	 * @see android.os.Parcelable#describeContents()
	 */
	@Override
	public int describeContents() {
		return this.hashCode();
	}

	/*
	 * Serialize my object attributes one by one
	 * 
	 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
	 */
	@Override
	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeString(this.connection);
		parcel.writeInt(this.id);
		parcel.writeString(this.email);
		parcel.writeString(this.mobile);
		parcel.writeString(this.name);
		parcel.writeString(this.picture);
		parcel.writeParcelable(this.online_person, 0);
	}

	/**
	 * Required for any Parcelable class to be recreated. Will be called to
	 * de-seralize the object.
	 * 
	 * @author Stefan de Bruijn
	 * 
	 */
	public static final Parcelable.Creator<Reference> CREATOR = new Parcelable.Creator<Reference>() {
		public Reference createFromParcel(Parcel source) {
			return new Reference(source);
		}

		public Reference[] newArray(int size) {
			return new Reference[size];
		}
	};

	/**
	 * <b>Do not use!</b> Private constructor for the Parcelable Creator. Will
	 * de-serialize the object.
	 * 
	 * @param source
	 */
	private Reference(Parcel source) {
		this.connection = source.readString();
		this.id = source.readInt();
		this.email = source.readString();
		this.mobile = source.readString();
		this.name = source.readString();
		this.picture = source.readString();

		this.online_person = source.readParcelable(OnlinePerson.class.getClassLoader());

	}

}
